package com.example.studentmanager;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class StudentDB extends SQLiteOpenHelper {
    private static final String DBname = "stuent.db";
    private static final String TableName = "Student";
    public static StudentDB studentDB = null;
    private SQLiteDatabase rdb = getReadableDatabase();
    private SQLiteDatabase wdb = getWritableDatabase();
    ;

    public StudentDB(@Nullable Context context) {
        super(context, DBname, null, 3);
    }

    public static StudentDB getStudentDB(Context context) {
        if (studentDB == null) {
            studentDB = new StudentDB(context);
        }
        return studentDB;
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        String sql = "Create table if not exists " + TableName +
                "("
                + "xuehao varchar not null UNIQUE,"
                + "gender varchar not null,"
                + "address varchar not null,"
                + "phone varchar not null,"
                + "chenji varchar not null"
                + ");";
        sqLiteDatabase.execSQL(sql);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL("DROP TABLE IF EXISTS " + TableName);
        onCreate(db);
    }


    public boolean insert(Student student) {

        SQLiteDatabase db = getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put("xuehao", student.getXuehao());
        values.put("gender", student.getGender());
        values.put("address", student.getAddress());
        values.put("phone", student.getPhone());
        values.put("chenji", student.getChengji());
        long reslut = db.insert(TableName, null, values);

        if (reslut == -1) {
            return false;
        } else {
            return true;
        }

    }

    public Student query(String xuehao) {

        Student student = new Student();
        Cursor cursor = rdb.query(TableName, null, "xuehao=?", new String[]{xuehao}, null, null, null);
        if (cursor.moveToNext()) {
            student.xuehao = cursor.getString(0);
            student.gender = cursor.getString(1);
            student.address = cursor.getString(2);
            student.phone = cursor.getString(3);
            student.chengji = cursor.getString(4);

        }
        if (student.xuehao != null) {
            return student;
        } else {
            return null;
        }

    }

    public List<Student> queryall() {

        String sql = "select * from " + TableName;
        List<Student> list = new ArrayList<>();
        Cursor cursor = rdb.rawQuery(sql, null);
        while (cursor.moveToNext()) {
            Student student = new Student();
            student.xuehao = cursor.getString(0);
            student.gender = cursor.getString(1);
            student.address = cursor.getString(2);
            student.phone = cursor.getString(3);
            student.chengji = cursor.getString(4);
            list.add(student);

        }
        cursor.close();
        return list;

    }

    public boolean updateById(String xuehao, String gender, String address, String phone, String chengji) {
        ContentValues values = new ContentValues();
        values.put("gender", gender);
        values.put("address", address);
        values.put("phone", phone);
        values.put("chenji", chengji);

        int update = wdb.update(TableName, values, "xuehao = ?", new String[]{xuehao});
        if (update > 0) {
            return true;
        } else {
            return false;
        }
    }

    public boolean deleteById(String xuehao) {
        int result = wdb.delete(TableName, "xuehao = ?", new String[]{xuehao});
        if (result == 1) {
            return true;
        } else {
            return false;
        }
    }

}